#include "common.h"

int main()
{
    cout << "this is write process!" << endl;

    key_t key = ftok(PATHNAME, PROJ_ID);

    //写端,直接打开这个消息队列
    int msgid = msgget(key, IPC_CREAT);
    if (msgid < 0)
    {
        perror("write error");
        return 1;
    }

    long msg_type;
    char str[1024] = {0};
    msg_struct msg;
    //int count = 5;
    while(1/*count--*/)
    {
        cout << "please input message type(end of 0) : ";
        cin >> msg_type; 
        cout << endl;
        if (msg_type == 0)
        {
            break;
        }
        cout << "please input message text : ";
        cin >> str;
        cout << endl;

        msg.msg_type = msg_type;
        strcpy(msg.text, str);
        /* 发送消息队列 
		*  当msgflg为0时，msgsnd()及msgrcv()
		*  在队列呈满或呈空的情形时，采取阻塞等待的处理模式。IPC_NOWAIT
		*/

        int ret_value = msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);
        if (ret_value < 0)
        {
            perror("msgsnd");
            return 2;
        }
        cout << "write -> " << "ret_value : " << ret_value << endl;
    }

    return 0;
}
